package com.pets.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pets.mapper.UsersMapper;
import com.pets.pojo.EquipmentManagement;
import com.pets.pojo.FeedbackManagement;
import com.pets.mapper.FeedbackManagementMapper;
import com.pets.pojo.Users;
import com.pets.service.IFeedbackManagementService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pets.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 反馈管理 服务实现类
 * </p>
 *
 * @author 小陳同學
 * @since 2024-05-13
 */
@Service
public class FeedbackManagementServiceImpl extends ServiceImpl<FeedbackManagementMapper, FeedbackManagement> implements IFeedbackManagementService {

    @Autowired
    private FeedbackManagementMapper feedbackManagementMapper;

    @Autowired
    private UsersMapper usersMapper;

    @Override
    public Result register(FeedbackManagement feedbackManagement) {
        if(feedbackManagement.getUserId() == null){
            return Result.fail("未设置userid");
        }
        if(feedbackManagement.getFeedbackInformation() == null){
            return Result.fail("未设置反馈类容");
        }
        feedbackManagement.setCreateTime(LocalDateTime.now());
        return feedbackManagementMapper.insert(feedbackManagement) > 0 ? Result.success("成功") : Result.fail("失败");
    }

    @Override
    public Result deleteById(Integer id) {
        int rows = feedbackManagementMapper.deleteById(id);
        return rows > 0 ? Result.success("删除成功") : Result.error("删除失败");
    }

    @Override
    public Result selectPage(Integer page, Integer pageSize, Integer userId) {
        final LambdaQueryWrapper<FeedbackManagement> lqw = new LambdaQueryWrapper<>();
        if(userId!=null){
            Users users = usersMapper.selectById(userId);
            if(users!=null){
                if(users.getUserType()==2||users.getUserType()==3){
                    lqw.eq(FeedbackManagement::getUserId, users.getUserId());
                }
            }
        }
        IPage<FeedbackManagement> Page = new Page<>(page, pageSize);
        IPage<FeedbackManagement> IPage = feedbackManagementMapper.selectPage(Page, lqw);
        return Result.success("查询成功", IPage);
    }
}
